/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package viewcontrollers.util;

import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * @author A7med Kotb
 * 
 */
public class XMLParser {

    private String supportMail;
    private String supportPassword;
    private String server;
    private String serverPort;
    private List expenseTypes;
    private List<String> expenses;
    private List<String> activities;

    public XMLParser() {
        
    }

    public List<String> getActivities() {
        return activities;
    }

    public void setActivities(List<String> activities) {
        this.activities = activities;
    }

    public XMLParser(String configFilePath) {
        parse(configFilePath);
    }

     public void parseActivities(String configFilePath) {
        try {
            List<String> activ=new ArrayList<String>();
            DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dbuilder = dfactory.newDocumentBuilder();
            Document xmldocument = dbuilder.parse(configFilePath);
            NodeList typeslist = xmldocument.getElementsByTagName("type");
            for (int i = 0; i < typeslist.getLength(); i++) {
                Element element = (Element) typeslist.item(i);
                activ.add(element.getTextContent());
            }
            System.out.println("###############Activ Size: "+activ.size());
            this.setActivities(activ);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    public void parseTypes(String configFilePath) {
        try {
            List<String> exp=new ArrayList<String>();
            DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dbuilder = dfactory.newDocumentBuilder();
            Document xmldocument = dbuilder.parse(configFilePath);
            NodeList typeslist = xmldocument.getElementsByTagName("type");
            for (int i = 0; i < typeslist.getLength(); i++) {
                Element element = (Element) typeslist.item(i);
                exp.add(element.getTextContent());
            }
            this.setExpenses(exp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void parse(String configFilePath) {
        try {
            DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dbuilder = dfactory.newDocumentBuilder();
            Document xmldocument = dbuilder.parse(configFilePath);
            Element settingelement = xmldocument.getDocumentElement();
            Element supportMailElement = (Element) settingelement.getElementsByTagName("Support-Mail").item(0);
            Element supportPasswordElement = (Element) settingelement.getElementsByTagName("Support-Password").item(0);
            Element serverElement = (Element) settingelement.getElementsByTagName("Server").item(0);
            Element serverPortElement = (Element) settingelement.getElementsByTagName("Port").item(0);
            NodeList typeslist = xmldocument.getElementsByTagName("type");
            this.setSupportMail(supportMailElement.getTextContent());
            this.setSupportPassword(supportPasswordElement.getTextContent());
            this.setServer(serverElement.getTextContent());
            this.setServerPort(serverPortElement.getTextContent());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<String> getExpenses() {
        return expenses;
    }

    public void setExpenses(List<String> expenses) {
        this.expenses = expenses;
    }
    
    public List getExpenseTypes() {
        return expenseTypes;
    }

    public void setExpenseTypes(List expenseTypes) {
        this.expenseTypes = expenseTypes;
    }

    public String getSupportMail() {
        return supportMail;
    }

    public void setSupportMail(String supportMail) {
        this.supportMail = supportMail;
    }

    public String getSupportPassword() {
        return supportPassword;
    }

    public void setSupportPassword(String supportPassword) {
        this.supportPassword = supportPassword;
    }

    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }

    public String getServerPort() {
        return serverPort;
    }

    public void setServerPort(String serverPort) {
        this.serverPort = serverPort;
    }
}
